package com.generator.mybaitsPlus;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.generator.AutoGenerator;
import com.generator.config.AbstractDbConfig;


/**
 * @author 大忽悠
 * @create 2022/9/15 10:11
 */
public class MpAutoGeneratorImpl implements AutoGenerator {
    private MpConfig mpConfig;

    public MpAutoGeneratorImpl(AbstractDbConfig mpConfig) {
          this.mpConfig = (MpConfig) mpConfig;
    }

    /**
     * 自动生成代码
     */
    @Override
    public void execute() {
        FastAutoGenerator
                //数据库配置
                .create(mpConfig.getUrl(), mpConfig.getUsername(), mpConfig.getPassword())
                //全局配置
                .globalConfig(builder -> {
                    builder.author("大忽悠")
                            //输出日期的格式
                            .commentDate("yyyy年MM月dd日")
                            //可以覆盖已经生成的文件，默认为false
                            .fileOverride()
                            //使用localDateTime
                            .dateType(DateType.TIME_PACK)
                            //禁止打开输出目录
                            .disableOpenDir()
                            //指定输出目录
                            .outputDir(mpConfig.getAutoGenerator().getOutPutPath())
                            .build();
                })
                //包配置
                .packageConfig(builder -> {
                    //父包名
                    builder.parent(mpConfig.getAutoGenerator().getParentModule())
                            //设置实体类包名
                            .entity("entity")
                            .mapper("mapper")
                            .build();
                })
                //策略配置
                .strategyConfig(builder -> {
                    //设置要映射的表
                    builder.addInclude(mpConfig.getAutoGenerator().getTableNames())
                            .build()
                            .entityBuilder()
                            .enableLombok();
                })
                //模板配置---不输出controller层和XML层
                .templateConfig(builder -> {
                    builder.disable(TemplateType.CONTROLLER,TemplateType.XML);
                })
                .execute();
    }
}
